<?php

require_once 'constantes.php';
require_once 'DaoConn.class.php';

class DaoLocations extends DaoConn{
		

	function getLocationAll() {
		$this->open();
		try {
			$sql = "SELECT * FROM ".LOCATIONS.";";
			$req = $this->bdd->prepare($sql);
			$req->execute();
			$tab = array();			
			while($result = $req->fetch(PDO::FETCH_OBJ)){
				array_push($tab, $result);				
			}
			$req->closeCursor();
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
		return $tab;
	}
	
	
	function getLocationsPayerbyIdClient($id) {
		$this->open();
		try {
			$sql = "SELECT * FROM ".LOCATIONS." WHERE ".LOCATIONS_LOC_CLI." =:idCli AND ".LOCATIONS_LOC_MONTANT." IS NOT NULL ;";
			$req = $this->bdd->prepare($sql);
			$req->execute(array('idCli'=>$id));
			$tab = array();			
			while($result = $req->fetch(PDO::FETCH_OBJ)){
				array_push($tab, $result);				
			}
			$req->closeCursor();
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
		return $tab;
	}
	
	
	/*
	 * faire la meme en haut avec 
	 * select * from 
films join locations on
films.f_id = loc_film
where loc_film = 2 and loc_montant is not null;
	 */
	
	function getFilmsLocationsPayerbyIdClient($id) {
		$this->open();
		try {
			$sql = "SELECT * FROM ".FILMS." JOIN ".LOCATIONS." ON ".FILMS_ID." = ".LOCATIONS_LOC_CLI." WHERE ".LOCATIONS_LOC_CLI." =:idCli AND ".LOCATIONS_LOC_MONTANT." IS NOT NULL ;";
			$req = $this->bdd->prepare($sql);
			$req->execute(array('idCli'=>$id));
			$tab = array();
			while($result = $req->fetch(PDO::FETCH_OBJ)){
				array_push($tab, $result);
			}
			$req->closeCursor();
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
		return $tab;
// return $sql;
	}
	/*
	 * TODO , getLocationNONPAYés, loc_montant non null
	 */
	
	function getNbLocationPayerbyIdClient($idClient){
		$this->open();
		try {
			$sql = "SELECT count(*) FROM ".LOCATIONS." WHERE ".LOCATIONS_LOC_CLI." =:idCli AND ".LOCATIONS_LOC_MONTANT." IS NOT NULL ;";
			$req = $this->bdd->prepare($sql);
			$nb = $req->execute(array('idCli'=>$idClient));
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
		return $nb;
	}
	
	
	function setLocation($tab,$idClient){
		$this->open();
		try {
			foreach($tab as $value){
				$sql = "INSERT INTO ".LOCATIONS." ( ".LOCATIONS_LOC_CLI." , ".LOCATIONS_LOC_FILM." ) VALUES ( ".$idClient." , ".$value." );";
				$req = $this->bdd->prepare($sql);
				$req->execute();
			}
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
	
	}
	
	
	function setPrixLocation($tab, $idClient){
		$this->open();
		try {
			foreach($tab as $value){
				$ele = explode('$$', $value);
				$sql = "UPDATE ".LOCATIONS." SET ".LOCATIONS_LOC_MONTANT." = ".floatval($ele[1])."WHERE ".LOCATIONS_LOC_FILM." =:idFilm AND ".LOCATIONS_LOC_CLI." =:idCli ;";
				$req = $this->bdd->prepare($sql);
				$req->execute(array('idFilm'=>$ele[0],'idCli'=>$idClient));
				echo floatval($ele[1])."<br/>";
			}
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
	}

}